<template>
  <div>
    <!-- 给action一个#号 就不会报错了 -->
    <!-- file-list是上传的文件列表 可以绑定到上传组件上，让上传组件来显示 -->
    <!-- upload组件显示的是file-list -->
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :on-preview="preview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :http-request="upload"
      :on-change="change"
      :on-success="success"
      :class="{disabled:disabled}"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPercent" :percentage="percent" style="width: 180px" />
    <el-dialog :visible.sync="dialogVisible" title="图片预览">
      <img :src="imgUrl" alt="" style="width:60%">
    </el-dialog>
  </div>

</template>
<script>
import COS from 'cos-js-sdk-v5'
export default {
  props: {
    staffPhoto: {
      type: String
    }
  },
  data() {
    return {
      fileList: [],
      imgUrl: '',
      dialogVisible: false,
      showPercent: false,
      percent: 0,
      disabled: true
      /* 控制+ 显示和隐藏
			1-初次进入   disabled：true
			2-点击删除图标按钮===on-remove  钩子中设置  + 显示：disabled：false
			3-点击+ 上传图片就需要隐藏+  =====选择图片后需要隐藏+     + 需要隐藏：on-change设置  disabled：true

			 */
    }
  },
  watch: {
    staffPhoto: {
      handler(newVal) {
        this.imgUrl = newVal //  imgUrl的值：没有选择上传图片 值就是传过来的，如果选了上传图片，值是cos返回来的
        console.log(this, 333)
        if (newVal) {
          this.disabled = true
          this.fileList = [{ url: newVal }]
        } else {
          this.disabled = false
        }
      },
      immediate: true
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList)
      this.disabled = false
    },
    preview(file) {
      // this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    change(file, fileList) {
      // console.log(file, fileList, 888777666)
      this.disabled = true

      this.fileList = this.fileList.map(item => {
        return file
      })
    },
    beforeUpload(file) {
      console.log(file, 888)
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      //  判断图片资源条件
      /* 1-判断选择图片的格式
				2-判断图片尺寸大小
				3-文件是否存在
			 */
      if (!this.fileList.length) {
        this.$message.error('请选择图片')
        return false
      }
      // if (file.type !== 'image/jpeg' || file.type !== 'image/jpg' || file.type !== 'image/png' || file.type !== 'image/gif') {
      if (!types.includes(file.type)) {
        // alert('请输入合法的图片格式')
        this.$message.error('请输入合法的图片格式')
        return false
      }
      if (file.size > 60000) {
        // this.$message({
        //   type: 'error',
        //   message: '您输入的图片尺寸太大了'
        // })
        this.$message.error('您输入的图片尺寸太大了')
        return false
      }

      this.showPercent = true //  显示进度条
      return true //  可以进入到下一步
    },
    upload(params) {
      console.log(params, 9988)
      //  函数一调用 已经向腾讯云发了请求
      var cos = new COS({
        SecretId: 'AKIDZx6yavzmIYYHjvqTpvjHYYOxFG1WkhNm',
        SecretKey: '91dx2rPhNmumNimfJhodCUWHm1zh9JDq'
      })
      cos.putObject({
        Bucket: '666-1302684573', //  存储桶
        /* 必须 */
        Region: 'ap-nanjing',
        /* 存储桶所在地域，必须字段 */
        Key: params.file.name,
        /* 必须 */
        StorageClass: 'STANDARD',
        Body: params.file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(this, 66655, 998811)
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        setTimeout(() => {
          //  关闭进度条
          this.showPercent = false
        }, 1000)
        // console.log(123)
        console.log(err || data, 333222111)
        this.imgUrl = 'https://' + data.Location// 收集像cos发送请求返回来的数据
      })
    },
    success() {

    }
  }
}

</script>

<style>
  .disabled .el-upload--picture-card {
    display: none
  }

</style>
